package com.atguigu.controller;


import com.alibaba.fastjson.JSONObject;
import com.atguigu.entity.*;
import com.atguigu.result.RetVal;
import com.atguigu.result.RetValCodeEnum;
import com.atguigu.service.BaseCategory1Service;
import com.atguigu.service.BaseCategory2Service;
import com.atguigu.service.BaseCategory3Service;
import com.atguigu.service.BaseCategoryViewService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 分级分类表 前端控制器
 * </p>
 * @author yangchaolove
 * @since 2021-11-06
 *
 * http://127.0.0.1:8200/product/getCategory1
 */
@RestController
@RequestMapping("/product")
//@CrossOrigin
public class CategoryController {

    @Autowired
    private BaseCategory1Service category1Service;
    @Autowired
    private BaseCategory2Service category2Service;
    @Autowired
    private BaseCategory3Service category3Service;
    @Autowired
    private BaseCategoryViewService categoryViewService;
    //查查询一级分类
    @GetMapping("/getCategory1")
    public RetVal getCategory1() {
        List<BaseCategory1> list = category1Service.list(null);
        return RetVal.ok(list);
    }
    // 获取二级分类数据
    @GetMapping("/getCategory2/{category1Id}")
    public RetVal getCategory2(@PathVariable Long category1Id){
        QueryWrapper<BaseCategory2> wrapper = new QueryWrapper<BaseCategory2>();
        wrapper.eq("category1_id", category1Id);
        List<BaseCategory2> category2List = category2Service.list(wrapper);
        return RetVal.ok(category2List);
    }
    // 获取三级分类数据
    @GetMapping("/getCategory3/{category2Id}")
    public RetVal getCategory3(@PathVariable Long category2Id){
        QueryWrapper<BaseCategory3> wrapper = new QueryWrapper<BaseCategory3>();
        wrapper.eq("category2_id", category2Id);
        List<BaseCategory3> category3List = category3Service.list(wrapper);
        return RetVal.ok(category3List);
    }


    /**
     *   查询所有的分类，及其子分类
     */
    @GetMapping("/list/tree")
    public RetVal list(){
        List<BaseCategoryEntity> entities=category1Service.listWithTree();
        return RetVal.ok(entities);
    }

    /**
     * 首页分类参数
     * @return
     */
    @GetMapping("getIndexCategoryInfo")
    public RetVal getIndexCategoryInfo(){
        List<JSONObject> categoryInfo =categoryViewService.getIndexCategoryInfo();
        return RetVal.ok(categoryInfo);
    }


}

